
Partial Class Posts
    Inherits System.Web.UI.Page

    Dim nid, strImg, strVid, anon As String
    Dim len As Integer

    Protected Sub Posts_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("type") <> "user" And Session("type") <> "admin" Then
            Response.Redirect("Error.aspx")
            Exit Sub
        ElseIf Session("type") = "user" Then
            lnkProfName.Text = "Logged in as " & Session("prim") & " (" & Session("sec") & ")"
        End If

        If Session("strImg") <> "" Then lblImgDone.Visible = True
        If Session("strVid") <> "" Then lblVidDone.Visible = True
        Session("now") = Date.Now.ToString("yyyyMMddHHmmss")

        Dim a As New Globo
        a.executesql("select * from countries order by country_name", False)
        ddlCon.Items.Clear()
        While a.dr.Read = True
            ddlCon.Items.Add(a.dr(0))
        End While
        ddlCon.SelectedIndex = 110


        Dim b As New Globo
        b.executesql("select * from types", False)
        ddlNType.Items.Clear()
        ddlNType.Items.Add("")
        While b.dr.Read = True
            ddlNType.Items.Add(b.dr(0))
        End While
        ddlNType.Text = ""
        ddlNType.Items.Add("Other")

        'DEBUG(mode)
        ddlNType.SelectedIndex = 11
        txtReg.Text = "reg"
        txtSReg.Text = "subreg"
        Dim c As New Globo
        c.executesql("select count(*) from posts", False)
        While c.dr.Read
            txtHeader.Text = "NewsHeaderSample" & (c.dr(0) + 1)
            txtBody.Text = "NewsBodySample" & (c.dr(0) + 1)
        End While
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim newsHeader As String
        Dim newsBody As String

        If ddlNType.Text = "" Then
            lblErr.Text = "Select your Newz Type"
            lblErr.Visible = True
            Exit Sub
        End If

        If ddlCon.Text = "" Then
            lblErr.Text = "Select Country"
            lblErr.Visible = True
            Exit Sub
        End If

        If txtHeader.Text = "" Then
            lblErr.Text = "Type the Newz Head"
            lblErr.Visible = True
            Exit Sub
        End If

        Dim d As New Globo
        d.executesql("select * from posts", False)
        While d.dr.Read
            If txtHeader.Text = d.dr(8) Then
                lblErr.Text = "News header alredy exists"
                lblErr.Visible = True
                Exit Sub
            Else
                newsHeader = txtHeader.Text
            End If
        End While

        If txtBody.Text = "" Then
            lblErr.Text = "Type your Newz"
            lblErr.Visible = True
            Exit Sub
        Else
            newsBody = Replace(txtBody.Text, "`", "'")
            newsBody = Replace(newsBody, vbCrLf, "`")
        End If

        nid = Session("now") + Session("prim")
        anon = chkAnon.Checked.ToString
        'If chkAnon.Checked = True Then
        'anon = True
        'Else
        'anon = False
        'End If


        'Data Insertion
        '*******************************************************
        Dim a As New Globo
        a.executesql("insert into posts values ('" & nid & "', '" & Session("prim") & "', '" & ddlNType.Text & "', '" & ddlCon.Text & "', '" & txtReg.Text & "', '" & txtSReg.Text & "', '" & Date.Today & "', '" & Date.Now.ToLongTimeString & "', '" & newsHeader & "', '" & newsBody & "', '" & "Available" & "', '" & "0" & "', '" & "0" & "', '" & anon & "', '" & "0" & "', '" & "0" & "')", True)
        '*******************************************************

        'image file upload
        Dim b As New Globo
        If Session("strImg") <> "" Then 'flImage.HasFile Then
            strImg = Session("strImg")
            b.executesql("insert into nimage values ('" & nid & "', '" & strImg & "')", True)
        End If

        'video file upload
        Dim c As New Globo
        If Session("strVid") <> "" Then 'flVid.HasFile Then
            strVid = Session("strVid")
            c.executesql("insert into nvid values ('" & nid & "', '" & strVid & "')", True)
        End If

        Session("NRload") = 0
        Session("strImg") = ""
        Session("strVid") = ""
        'Response.Redirect("NewsRoom.aspx")
        Response.Redirect("Posts.aspx")
        'Server.Transfer("NewsRoom.aspx")
    End Sub

    Protected Sub btnImgUL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImgUL.Click
        'If Me.IsPostBack = False Then
        If flImage.HasFile Then
            strImg = Session("now") + Session("prim")  'Date.Now.ToString("yyyyMMddHHmmss")
            Session("strImg") = strImg
            lblErr.Text = "Uploaded as '" & Session("strImg") & "'"
            lblErr.Visible = True

            Dim fl(), fil As String
            fl = flImage.FileName.Split("\")
            fil = fl(fl.Length - 1)
            fl = fil.Split(".")
            flImage.SaveAs(Server.MapPath("~/Upload_Pic/") & strImg & "." & fl(fl.Length - 1))
            strImg = "~/Upload_Pic/" + strImg

            flImage.Enabled = False
            btnImgUL.Enabled = False
            lblImgDone.Visible = True
        Else

        End If
        'End If
    End Sub

    Protected Sub btnVidUL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVidUL.Click
        'If Me.IsPostBack = False Then
        If flVid.HasFile Then
            strVid = Session("now") + Session("prim")  'Date.Now.ToString("yyyyMMddHHmmss")
            Session("strVid") = strVid
            lblErr.Text = "Uploaded as '" & Session("strVid") & "'"
            lblErr.Visible = True

            Dim fl(), fil As String
            fl = flVid.FileName.Split("\")
            fil = fl(fl.Length - 1)
            fl = fil.Split(".")
            flVid.SaveAs(Server.MapPath("~/Upload_Vid/") & strVid & "." & fl(fl.Length - 1))
            strVid = "~/Upload_Pic/" + strVid

            flVid.Enabled = False
            btnVidUL.Enabled = False
            lblVidDone.Visible = True
        Else

        End If
            'End If
    End Sub

    Protected Sub txtBody_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBody.TextChanged
        'len = 4000 - txtBody.Text.Length 'rkd
        'lblCharLeft.Text = "Characters left: " & len
    End Sub

    '_________________________________________________________________________________________________________________________________
    '#################################################################################################################################
    Protected Sub lnkProfName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkProfName.Click
        If Session("type") <> "" Then
            Dim str As String
            str = UCase(Left(Session("type"), 1))
            Response.Redirect("" & str & "Profile.aspx")
        End If
    End Sub

    'Protected Sub lnkQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkQuery.Click
    '    Response.Redirect("Query.aspx")
    'End Sub

    Protected Sub lnkThemes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkThemes.Click
        Response.Redirect("Themes.aspx")
    End Sub

    Protected Sub lnkSettings_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSettings.Click
        Response.Redirect("Settings.aspx")
    End Sub

    Protected Sub lnkLogOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLogOut.Click
        Server.Transfer("Loading.aspx")
    End Sub

    '_____________________________________________________________________________________________________________
    '*************************************************************************************************************
    Protected Sub lnkAboutUs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAboutUs.Click
        Response.Redirect("AboutUs.aspx")
    End Sub

    Protected Sub lnkTermCond_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTermCond.Click
        Response.Redirect("TermsConditions.aspx")
    End Sub

    Protected Sub lnkContactUs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkContactUs.Click
        Response.Redirect("ContactUs.aspx")
    End Sub

    Protected Sub lnkFAQs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFAQs.Click
        Response.Redirect("FAQs.aspx")
    End Sub

    Protected Sub lnkFeedback_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFeedback.Click
        Response.Redirect("Feedback.aspx")
    End Sub
End Class